package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.system.domain.SysTable;
import com.ruoyi.system.service.ISysTableService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysTableColumn;
import com.ruoyi.system.service.ISysTableColumnService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 字段描述Controller
 * 
 * @author ruoyi
 * @date 2024-03-26
 */
@RestController
@RequestMapping("/system/column")
public class SysTableColumnController extends BaseController
{
    @Autowired
    private ISysTableColumnService sysTableColumnService;


    /**
     * 查询字段描述列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysTableColumn sysTableColumn)
    {
        startPage();
        List<SysTableColumn> list = sysTableColumnService.selectSysTableColumnList(sysTableColumn);
        return getDataTable(list);
    }

    /**
     * 导出字段描述列表
     */
    @PreAuthorize("@ss.hasPermi('system:column:export')")
    @Log(title = "字段描述", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTableColumn sysTableColumn)
    {
        List<SysTableColumn> list = sysTableColumnService.selectSysTableColumnList(sysTableColumn);
        ExcelUtil<SysTableColumn> util = new ExcelUtil<SysTableColumn>(SysTableColumn.class);
        util.exportExcel(response, list, "字段描述数据");
    }

    /**
     * 获取字段描述详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:column:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysTableColumnService.selectSysTableColumnById(id));
    }

    /**
     * 新增字段描述
     */
    @PreAuthorize("@ss.hasPermi('system:column:add')")
    @Log(title = "字段描述", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTableColumn sysTableColumn)
    {
        SqlUtil.filterKeyword(sysTableColumn.getColumnName());
        SqlUtil.filterKeyword(sysTableColumn.getColumnComment());

        LoginUser loginUser = getLoginUser();
        sysTableColumn.setCreateBy(loginUser.getUsername());
        sysTableColumn.setColumnType("varchar(" + sysTableColumn.getLen() + ")");
        sysTableColumn.setQueryType("EQ");

        int num = sysTableColumnService.insertSysTableColumn(sysTableColumn);

        if(num == 0) {
            return error("该字段已存在");
        }

        return success(num);
    }

    /**
     * 修改字段描述
     */
    @PreAuthorize("@ss.hasPermi('system:column:edit')")
    @Log(title = "字段描述", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTableColumn sysTableColumn)
    {
        return toAjax(sysTableColumnService.updateSysTableColumn(sysTableColumn));
    }

    @PreAuthorize("@ss.hasPermi('system:column:edit')")
    @Log(title = "字段描述", businessType = BusinessType.UPDATE)
    @PutMapping("/editBatch")
    public AjaxResult editBatch(@RequestBody List<SysTableColumn> columns)  {
        for(int i=0; i<columns.size(); i++) {
            SysTableColumn column = columns.get(i);
            long sort = i + 2;
            column.setSort(sort);
            sysTableColumnService.updateSysTableColumn(column);
        }
        // return toAjax(sysTableColumnService.updateSysTableColumn(sysTableColumn));
        return success();
    }

    /**
     * 删除字段描述
     */
    @PreAuthorize("@ss.hasPermi('system:column:remove')")
    @Log(title = "字段描述", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids, Long tableId)
    {
        return toAjax(sysTableColumnService.deleteSysTableColumnByIds(ids, tableId));
        // return success();
    }
}
